package org.neuedu.servlet;

import org.neuedu.bean.*;
import org.neuedu.service.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ForwardUserIndexServlet",urlPatterns = "/user/index")
public class ForwardUserIndexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("fly_user");
        UserIndexInfo userIndexInfo = new UserIndexInfo();
        // 获取发表文章列表
        UserService service = new UserServiceImpl();
        List<Article> list1 = service.userArticle(user.getId());
        ArticleService articleService = new ArticleServiceImpl();
        int i = articleService.serchArticleCountByid(user.getId());
        Article article = new Article();
        article.setList(list1);
        article.setCount(i);
        // 获取收藏文章列表
        CollectService collectService = new CollectServiceImpl();
        Collect collect = new Collect();
        int count = collectService.serchCollectArticleByUid(user.getId());
        List<Collect> collectList = collectService.serchCollectArticle(user.getId());
        collect.setList(collectList);
        collect.setCount(count);
        userIndexInfo.setCollect(collect);
        userIndexInfo.setArticle(article);
        request.setAttribute("index_list",userIndexInfo);
        request.getRequestDispatcher("/WEB-INF/jsp/user/index.jsp").forward(request,response);
    }
}
